﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using JRCore.enums;
using JRCore.tools;
using System.Threading;
using System.Data;
using DataRecv.db;
using JRCore.protocol;

namespace DataRecv.tools
{
    /// <summary>
    /// 失败重传封装
    /// </summary>
    public class ReUpload
    {
        /// <summary>
        /// 扫描线程
        /// </summary>
        private Thread m_ScanThread;
        /// <summary>
        /// 是否是卡口，true-是卡口。false-是电警
        /// </summary>
        private bool m_isKK;
        /// <summary>
        /// 推出标记
        /// </summary>
        private bool m_exit;
        private const string c_TableName = "FailRecord_t";
        private const string c_Columns = "P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20,P21,P22,P23,P24,P25,P26,P27,P28,P29,P30,P31,P32,P33,P34,P35,P36,P37,P38,P39,P40,P41,P42,P43,P44,P45,P46,P47,P48,P49,P50,P51,P52,P53,uuid";
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="isKK">否是卡口，true-是卡口，false-是电警</param>
        public ReUpload(bool isKK)
        {
            m_isKK = isKK;
            m_exit = false;
            m_ScanThread = new Thread(new ThreadStart(ScanThreadFunc));
            m_ScanThread.IsBackground = true;
            m_ScanThread.Start();
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Dispose()
        {
            m_exit = true;
            try
            {
                if (m_ScanThread != null)
                {
                    m_ScanThread.Abort();
                    m_ScanThread.Join(1000);
                }
            }
            catch { }
        }

        public void ScanThreadFunc()
        {
            DataTable dt = null;
            
            try
            {
                while(!m_exit)
                {
                    Thread.Sleep(20000);
                    try
                    {
                        string condition = (m_isKK ? "P5='0'" : "P5<>'0'");
                        dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, condition);
                        if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0))
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                if (m_exit == true) break;
                                if ((m_isKK == true) && (G.SystemParam.KKUploadEnable == false)) break;
                                if ((m_isKK == false) && (G.SystemParam.DJUploadEnable == false)) break;
                                Thread.Sleep(1000);
                                #region try each row
                                try
                                {
                                    string uuid = dt.Rows[i]["uuid"].ToString();
                                    DataRecvRecord record = new DataRecvRecord();
                                    for (int m = 0; m < DataRecvRecord.N; m++)
                                    {
                                        record.P[m] = dt.Rows[i][m].ToString();
                                    }
                                    if (record.P26 == "") record.P26 = "0";
                                    if (record.P27 == "") record.P27 = "0";

                                    Msg msg = new Msg();
                                    msg.type = Enum_ProtocolType.Upload;
                                    msg.uuid = Guid.NewGuid().ToString("N");
                                    msg.feedback = true;
                                    msg.data = record.ToBytes();
                                    msg.length = msg.data.Length;

                                    if (m_isKK == true)
                                    {
                                        if (G.KKClient != null)
                                        {
                                            string error = "";
                                            bool send = G.KKClient.Send(msg, ref error);
                                            if (LogTool.gWorkEnable) LogTool.log.Info((send ? "卡口重传成功" : "卡口重传失败") + record.P19 + record.P15 + error);
                                            if (send == true)
                                            {
                                                string deletecondition = "uuid='" + uuid + "'";
                                                bool del = DBHelper.GetInstance().Delete(c_TableName, deletecondition);
                                                if (LogTool.gDebugEnable) LogTool.log.Info(del ? "删除卡口重传记录成功" : "删除卡口重传记录失败");
                                            }
                                            
                                        }
                                    }
                                    else
                                    {
                                        if (G.DJClient != null)
                                        {
                                            string error = "";
                                            bool send = G.DJClient.Send(msg, ref error);
                                            if (LogTool.gWorkEnable) LogTool.log.Info((send ? "电警重传成功" : "电警重传失败") + record.P19 + record.P15 + error);
                                            if (send == true)
                                            {
                                                string deletecondition = "uuid='" + uuid + "'";
                                                bool del = DBHelper.GetInstance().Delete(c_TableName, deletecondition);
                                                if (LogTool.gDebugEnable) LogTool.log.Info(del ? "删除电警重传记录成功" : "删除电警重传记录失败");
                                            }
                                            
                                        }
                                    }
                                }
                                catch (Exception er)
                                {
                                    LogTool.log.Info("ReUpload.TryReLoadEachRow()" + er.Message);
                                }
                                #endregion
                            }
                        }
                    }
                    catch (Exception er)
                    {
                        LogTool.log.Info("ReUpload.TryEachScan()" + er.Message);
                    }
                }
            }
            catch(Exception er)
            {
                LogTool.log.Info("ReUpload.ScanThreadFunc()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
        }

    }
}
